﻿using Microsoft.AspNetCore.Mvc;
using MiniFox.Components;
using MiniFox.Infrastructure;
using MiniFox.Platform.Exceptions;
using MiniFox.Platform.File.Models;
using MiniFox.Platform.File.Services;
using MiniFox.Resources;
using MiniFox.Utility;
using MiniFox.Web.Infrastructure;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MiniFox.Platform.File.Web
{
    /// <summary>
    /// 
    /// </summary>
    [NoCache, PublicAccessible]
    public class StorageController : MvcControllerBase
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="group"></param>
        /// <returns></returns>
        protected virtual IStorageService CreateComponent(string group)
        {
            IStorageService _component = ComponentFactory.GetComponent<IStorageService>(group);
            return _component;
        }

        /// <summary>
        /// 上传文件接口(url: /file/upload/{group})
        /// </summary>
        /// <param name="group">资源管理组名(对照OSS桶名)</param>
        /// <param name="path"></param>
        /// <returns>返回文件信息集合</returns>
        [HttpPost]
        public virtual async Task<Response<IEnumerable<FileData>>> UploadAsync(
            [Required(ErrorMessageResourceType = typeof(CommonMessages), ErrorMessageResourceName = "MSG_CommonRequired"), FromRoute(Name = GlobalKey.NAME)] string group, [FromQuery] string? path = null)
        {
            var fileDatas = await this.Request.HttpFileBatchLoadAsync();
            var component = this.CreateComponent(group);
            var result = await component.UploadAsync(path, fileDatas);
            var response = result.CreateResponse();

            return response;
        }

        /// <summary>
        /// presave后的确认操作，保存文件接口(url: /file/save/{group})
        /// </summary>
        /// <param name="group">资源管理组名(对照OSS桶名)</param>
        /// <param name="fileIds">已经id集合</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response> AcceptAsync([Required(ErrorMessageResourceType = typeof(CommonMessages), ErrorMessageResourceName = "MSG_CommonRequired"), FromRoute(Name = GlobalKey.NAME)] string group, [FromBody] string[] fileIds)
        {
            Response response = new Response();
            try
            {
                var component = this.CreateComponent(group);
                await component.AcceptAsync(fileIds);
            }
            catch (PlatformException e)
            {
                response.Code = e.ErrorCode;
                response.Message = e.Message;
            }
            return response;
        }

        /// <summary>
        /// 删除文件接口(url: /file/remove/{group})
        /// </summary>
        /// <param name="group">资源管理组名(对照OSS桶名)</param>
        /// <param name="fileIds">已经id集合</param>
        /// <returns></returns>
        [HttpPost]
        public Response Remove([Required(ErrorMessageResourceType = typeof(CommonMessages), ErrorMessageResourceName = "MSG_CommonRequired"), FromRoute(Name = GlobalKey.NAME)] string group, [FromBody] string[] fileIds)
        {
            Response response = new Response();
            try
            {
                var component = this.CreateComponent(group);
                component.Remove(fileIds);
            }
            catch (PlatformException e)
            {
                response.Code = e.ErrorCode;
                response.Message = e.Message;
            }
            return response;
        }
        /// <summary>
        /// 重命名文件接口(url: /file/rename/{group})
        /// </summary>
        /// <param name="group">资源管理组名(对照OSS桶名)</param>
        /// <param name="request">已经id集合</param>
        /// <returns></returns>
        [HttpPost]
        public Response Rename([Required(ErrorMessageResourceType = typeof(CommonMessages), ErrorMessageResourceName = "MSG_CommonRequired"), FromRoute(Name = GlobalKey.NAME)] string group, [FromBody] FileOperation request)
        {
            Response response = new Response();
            try
            {
                var component = this.CreateComponent(group);
                component.Rename(request.FileId, request.NewName);
            }
            catch (PlatformException e)
            {
                response.Code = e.ErrorCode;
                response.Message = e.Message;
            }
            return response;
        }
    }
}
